﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BoneImp {
	public class AnimationStateData {
		public SkeletonData skeletonData;
		List<FromEntry> entries = new List<FromEntry>();

		public AnimationStateData(SkeletonData skeletonData) {
			this.skeletonData = skeletonData;
		}

		public void setMixByName(string fromName, string toName, float duration) {
			Animation from = this.skeletonData.findAnimation(fromName);
			if (from == null) return;
			Animation to = this.skeletonData.findAnimation(toName);
			if (to == null) return;
			this.setMix(from, to, duration);
		}
		
		public void setMix(Animation from, Animation to, float duration) {
			/* Find existing FromEntry. */
			FromEntry fromEntry = FindFromEntry(from);
			if (fromEntry == null) {
				fromEntry = new FromEntry(from);
				this.entries.Add(fromEntry);
			}
			ToEntry toEntry = fromEntry.FindToEntry(to);
			if (toEntry != null) {
				toEntry.duration = duration;
				return;
			}
			toEntry = new ToEntry(to, duration);
			fromEntry.AddToEntry(toEntry);
		}

		private FromEntry FindFromEntry(Animation from) {
			foreach (FromEntry entry in this.entries) {
				if (entry.animation == from) {
					return entry;
				}
			}
			return null;
		}

		/* Returns 0 if there is no mixing between the animations. */
		public float getMix(Animation from, Animation to) {
			FromEntry fromEntry = FindFromEntry(from);
			if (from == null) {
				return 0;
			}
			ToEntry toEntry = fromEntry.FindToEntry(to);
			if (toEntry == null) {
				return 0;
			}
			return toEntry.duration;
		}
	}
}
